﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Content.Links;

namespace Marbles.Web.Admin.Content
{
    public partial class LinkMgr : ContentBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            LinkCollection items = new LinkCollection(this.Page.CurrentWeb.SiteId);
            cGrdFiles.DataSource = items;
            cGrdFiles.DataBind();
        }


        protected void Page_Command(object sender, CommandEventArgs e)
        {
            Link link = null;

            switch (e.CommandName)
            {
                case "New":
                    cDlgLinkEdit.Title = "Create new Favourite";
                    cDlgLinkEdit.Visible = true;
                    cBtnSave.CommandName = "Create";
                    cLblLastAccess.Text = "";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    cBtnDelete.Visible = false;
                    break;
                case "Create":
                    link = Link.CreateNew(this.CurrentWeb.SiteId);                    
                    link.Description = cTxtDescription.Text.Trim();
                    link.Url = cTxtLink.Text.Trim();
                    link.Title = cTxtTitle.Text.Trim();
                    link.Insert();
                    this.cDlgLinkEdit.Visible = false;
                    LoadData();
                    break;
                case "Modify":
                    link = new Link(this.CurrentWeb.SiteId, e.CommandArgument.ToString());
                    cDlgLinkEdit.Title = "Modify Existing Favourite";
                    cBtnSave.CommandName = "Update";
                    cBtnSave.CommandArgument = e.CommandArgument.ToString();
                    cBtnDelete.CommandArgument = e.CommandArgument.ToString();
                    cBtnDelete.Visible = true;
                    cTxtDescription.Text = link.Description;
                    cLblLastAccess.Text = link.LastAccessDate;
                    cTxtLink.Text = link.Url;
                    cTxtTitle.Text = link.Title;
                    cDlgLinkEdit.Visible = true;
                    break;
                case "Cancel":
                    this.cDlgLinkEdit.Visible = false;
                    cBtnSave.CommandName = "Create";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    break;
                case "Update":
                    link = new Link(this.CurrentWeb.SiteId, e.CommandArgument.ToString());
                    link.Description = cTxtDescription.Text.Trim();
                    link.Url = cTxtLink.Text.Trim();
                    link.Title = cTxtTitle.Text.Trim();
                    link.Update();
                    cBtnSave.CommandName = "Create";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    cDlgLinkEdit.Visible = false;
                    LoadData();
                    break;
                case "Delete":
                    link = new Link(this.CurrentWeb.SiteId, e.CommandArgument.ToString());
                    link.Delete();
                    cDlgLinkEdit.Visible = false;
                    LoadData();
                    break;
            }
        }
    }
}